/*
 * LUA_Pixmap.cpp
 *
 *  Created on: 07.02.2010
 *      Author: sven
 */

#include "LUA_Pixmap.h"
#include "LUA_util.h"

void checkChangesPixmap(pixmapData* data){
	if(data->changed == true){
			ilBindImage(data->imgID);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
					ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), IL_UNSIGNED_BYTE,
					ilGetData());
			data->changed = false;
	}
}

int drawPixmap(lua_State *L)
{
	pixmapData* data = (pixmapData*)getClass(L, TEXT("Data.Pixmap"));

	glBindTexture(GL_TEXTURE_2D, data->glTex);

	checkChangesPixmap(data);

	glLoadIdentity();
	glTranslatef(data->posX, data->posY, 0.0f);

	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_QUADS);
	glTexCoord2i(0, 0); glVertex2i(0,   0);
	glTexCoord2i(0, 1); glVertex2i(0,   data->height);
	glTexCoord2i(1, 1); glVertex2i(data->width, data->height);
	glTexCoord2i(1, 0); glVertex2i(data->width, 0);
	glEnd();

	return 0;
}

int drawPixmapPos(lua_State *L)
{
	float posY = lua_tonumber(L,-2);
	float posX = lua_tonumber(L,-3);

	pixmapData* data = (pixmapData*)getClass(L, TEXT("Data.Pixmap"));

	glBindTexture(GL_TEXTURE_2D, data->glTex);

	checkChangesPixmap(data);

	glLoadIdentity();
	glTranslatef(posX, posY, 0.0f);

	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_QUADS);
	glTexCoord2i(0, 0); glVertex2i(0,   0);
	glTexCoord2i(0, 1); glVertex2i(0,   data->height);
	glTexCoord2i(1, 1); glVertex2i(data->width, data->height);
	glTexCoord2i(1, 0); glVertex2i(data->width, 0);
	glEnd();

	return 0;
}


int setPixelPixmap(lua_State *L)
{
	int pixX = lua_tointeger(L, -6);
	int pixY = lua_tointeger(L, -5);
	int R = lua_tointeger(L, -4);
	int G = lua_tointeger(L, -3);
	int B = lua_tointeger(L, -2);
	int A = lua_tointeger(L, -1);
	ILubyte bc[4];
	bc[0] = (ILubyte)R;
	bc[1] = (ILubyte)G;
	bc[2] = (ILubyte)B;
	bc[3] = (ILubyte)A;

	pixmapData* data = (pixmapData*)getClass(L, TEXT("Data.Pixmap"));
	ilBindImage(data->imgID);
	ilSetPixels(pixX, pixY, 0, 1, 1, 1, IL_RGBA, IL_UNSIGNED_BYTE, bc);

	data->changed = true;

	return 0;
}



int deletePixmap(lua_State *L)
{
	return 0;
}



int getPixelPixmap(lua_State *L)
{
	int pixX = lua_tointeger(L, -2);
	int pixY = lua_tointeger(L, -1);

	pixmapData* data = (pixmapData*)getClass(L, TEXT("Data.Pixmap"));
	ilBindImage(data->imgID);

	ILubyte bc[4];
	ilCopyPixels(pixX, pixY, 0, 1, 1, 1, IL_RGBA, IL_UNSIGNED_BYTE,bc);

	lua_pushinteger(L, bc[0]);
	lua_pushinteger(L, bc[1]);
	lua_pushinteger(L, bc[2]);
	lua_pushinteger(L, bc[3]);

	return 4;
}



int loadPixmap(lua_State *L)
{
	const char* imgPath = lua_tostring(L, -1);
	//lua_pop(L,1);

	ILuint img;
	GLuint tex;
	int width;
	int height;

	ilGenImages(1, &img);
	ilBindImage(img);
	ilLoadImage(imgPath);

	ILuint devilError = ilGetError();
	if(devilError != IL_NO_ERROR){
		if(devilError == IL_COULD_NOT_OPEN_FILE)
			LUAERROR("could not open File");
		else LUAERROR("an error occured while loading an image");
		SDL_Quit();
	}
	ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

	width = ilGetInteger(IL_IMAGE_WIDTH);
	height = ilGetInteger(IL_IMAGE_HEIGHT);

	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
			ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), IL_UNSIGNED_BYTE,
			ilGetData());

	pixmapData* data = (pixmapData*)createClass(L, sizeof(pixmapData), TEXT("Data.Pixmap"));
	data->imgID = img;
	data->glTex = tex;
	data->height = height;
	data->width = width;
	data->posX = 0;
	data->posY = 0;

	return 1;
}

